//  ActivityViewAdvertisingCell.swift
//  iModern
//
//  Created by Ralf on 15/5/14.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class ActivityViewAdvertisingCell: AdvertisingCell
{
    var activity: NSMutableDictionary!
    override func awakeFromNib()
    {
        super.awakeFromNib()
        initLabel()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func setData(data: NSMutableDictionary)
    {
        activity = data

        let advertising = data["article"] as! NSDictionary
        let contact     = advertising["contact"] as! NSDictionary

        //let alias   = contact["alias"] as! String
        //let advert  = advertising["summary"] as! String
        //let message = advertising["message"] as! String


        
        setAliasLabel(contact["alias"] as! String)
        setAvatarImageView(contact["thumbnailURL"] as? String)

        setPreviewImageView(advertising["imageURL"] as! String)
        setSummaryLabel(advertising["summary"] as! String)
        setMessageTextView(advertising["message"] as! String)

        let numberOfFavorites = advertising["numberOfFavorites"] as? Int
        setNumberOfFavoritesButton(String(numberOfFavorites!))
        let price = advertising["price"] as? Int
        setBuyButton(price!, buttonData: activity)

        let button = viewWithTag(__number_of_favorites_button_tag) as! UIButton
        button.addTarget(self, action: "favorites:", forControlEvents: .TouchUpInside)
        button.imageEdgeInsets = UIEdgeInsetsMake(7, 7, 7, 25)

        if appDelegate.isLogged()
        {
            let favorites = Bool(activity["favorites"] as! Int)
            if favorites
            {
                button.setImage(UIImage(named: "Favorited-Small"), forState: .Normal)
            }
            else
            {
                button.addTarget(self, action: "favorites:", forControlEvents: .TouchUpInside)
            }

        }

    }

    func getNumberOfFavoritesButton() -> UIButton
    {
        return self.viewWithTag(__number_of_favorites_button_tag) as! UIButton
    }

    func loadImage(article: NSMutableDictionary)
    {
        let ivPreview = self.viewWithTag(__preview_image_view_tag) as! UIImageView
        let imageURL  = article["imageURL"] as! String
        ivPreview.load("\(appDelegate.parameterManager.server)/\(imageURL)", placeholder: nil)
    }

    @IBAction func favorites(sender: AnyObject)
    {
        if !appDelegate.isLogged()
        {
            login()
        }
        else
        {
            let button = self.viewWithTag(__number_of_favorites_button_tag) as! UIButton

            if !button.selected
            {
                activity["favorites"] = true
                let favoritesManager = FavoritesManager()
                let article          = activity["article"] as! NSMutableDictionary
                let articleId        = String(article["id"] as! Int)
                favoritesManager.addFavorites(articleId)
                //var numberOfFavorites = article["numberOfFavorites"] as! Int
//                article["numberOfFavorites"] = numberOfFavorites + 1

                NSNotificationCenter.defaultCenter().postNotificationName("Refresh Market", object: nil)
            }
            else
            {

            }
            NSNotificationCenter.defaultCenter().postNotificationName("Refresh Market", object: nil)

        }
    }
    
    func initLabel()
    {
        let screen = UIScreen.mainScreen().bounds
        
//        let y     = bounds.height - 35
//        let x     = bounds.width - 145
//        let frame = CGRect(x: x, y: y, width: 140, height: 30)
        
        var label      = UILabel()
        let labelFrame = CGRect(x: 0, y: 1, width: screen.size.width, height: 1)
        label = UILabel(frame: labelFrame)
        label.backgroundColor = UIColor.groupTableViewBackgroundColor()
        self.addSubview(label)
    }
}
